﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class XordersBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private string currentWhName;
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IXorderDetailDA XorderDetailDA = DataAccess.CreateXorderDetailDA();
        private static readonly IXordersDA XordersDA = DataAccess.CreateXordersDA();
        private static readonly IXpickDetailDA xpickDetailDA = DataAccess.CreateXpickDetailDA();

        public XordersBLL()
        {
            this.pubParasInfo = null;
            this.currentWhName = string.Empty;
        }

        public XordersBLL(string currentWH)
        {
            this.pubParasInfo = null;
            this.currentWhName = string.Empty;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.currentWhName = currentWH;
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public XordersBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            this.currentWhName = string.Empty;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.currentWhName = tempPubParasInfo.CurrentWH;
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteXorders(List<string> orderID)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = "";
            try
            {
                if (XordersDA.DeleteXorders(dataBase, tran, orderID, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; orderID.Count > i; i++)
                    {
                        temp = temp + orderID[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetXordersByQueryEntity(XordersQueryEntity XordersQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(XordersQuery.PoID))
            {
                sqlWhere.Add(" PO_ID " + XordersQuery.PoID);
            }
            if (!string.IsNullOrEmpty(XordersQuery.OrderID))
            {
                sqlWhere.Add(" ORDER_ID" + XordersQuery.OrderID);
            }
            if (!string.IsNullOrEmpty(XordersQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID" + XordersQuery.OwnerID);
            }
            return XordersDA.GetXordersByQueryList(sqlWhere, XordersQuery, this.dataBaseID[0]);
        }

        public XordersInfo GetXordersInfoByID(string XordersID)
        {
            XordersInfo XordersInfo = XordersDA.GetXordersByID(XordersID, this.dataBaseID[0]);
            if (XordersInfo != null)
            {
                XordersInfo.XOrderDetailInfos = XorderDetailDA.GetXorderDetailListByID(XordersID, this.dataBaseID[0]);
                XordersInfo.XpickDetailInfos = xpickDetailDA.GetXpickDetailListByID(XordersID, this.dataBaseID[0]);
            }
            return XordersInfo;
        }

        public DataSet GetXordersReceiptPurchaseOrderByQueryList(XordersQueryEntity xordersQueryEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(xordersQueryEntity.PoID))
            {
                sqlWhere.Add(" R.PO_ID " + xordersQueryEntity.PoID);
            }
            if (!string.IsNullOrEmpty(xordersQueryEntity.ReceiptID))
            {
                sqlWhere.Add(" R.RECEIPT_ID" + xordersQueryEntity.ReceiptID);
            }
            if (!string.IsNullOrEmpty(xordersQueryEntity.OwnerID))
            {
                sqlWhere.Add(" R.OWNER_ID" + xordersQueryEntity.OwnerID);
            }
            return XordersDA.GetXordersReceiptPurchaseOrderByQueryList(sqlWhere, xordersQueryEntity, this.dataBaseID[0]);
        }

        public string InsertXorders(XordersInfo XordersInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string XordersID = string.Empty;
            string whLoginID = this.dataBaseID[0];
            try
            {
                if (!DataAccess.CreateOwnerDA().CheckTradePartnerIDUnique(XordersInfo.OwnerID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F4200_004", this.pubParasInfo.UserLanguage, new List<string> { XordersInfo.OrderID });
                }
                else if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, XordersInfo.OrderID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F4200_005", this.pubParasInfo.UserLanguage, new List<string> { XordersInfo.OrderID });
                }
                if (!string.IsNullOrEmpty(XordersInfo.OrderID))
                {
                    if (XordersDA.CheckXordersIDUnique(dataBase, tran, XordersInfo.OrderID, whLoginID))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F4200_007", this.pubParasInfo.UserLanguage, new List<string> { XordersInfo.OrderID });
                    }
                    else
                    {
                        XordersID = XordersInfo.OrderID;
                    }
                }
                else
                {
                    XordersID = commonMethod.GenNextNumStr(dataBase, tran, "XORDERS", whLoginID);
                }
                XordersInfo.OrderID = XordersID;
                if (XordersDA.InsertXorders(dataBase, tran, XordersInfo, whLoginID) > 0)
                {
                    if ((XordersInfo.XOrderDetailInfos != null) && (XordersInfo.XOrderDetailInfos.Count > 0))
                    {
                        foreach (XorderDetailInfo info in XordersInfo.XOrderDetailInfos)
                        {
                            info.OrderID = XordersID;
                            info.CreatedBy = this.pubParasInfo.UserID;
                            info.UpdatedBy = this.pubParasInfo.UserID;
                            XorderDetailDA.InsertXorderDetail(dataBase, tran, info, whLoginID);
                        }
                    }
                    XordersID = XordersInfo.OrderID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return XordersID;
        }

        public string UpdateXOrdres(XordersInfo XordersInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string XordersID = string.Empty;
            try
            {
                if (XordersDA.UpdateXorders(dataBase, tran, XordersInfo, this.dataBaseID[0]) > 0)
                {
                    XordersID = XordersInfo.OrderID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return XordersID;
        }
    }
}

